package com.hw.kms.keyfile;

import java.io.File;
import java.io.FileOutputStream;

import sun.misc.BASE64Encoder;

import com.crb.kms.hsm.codec.HsmSJJ1312BUtil;
import com.crb.kms.hsm.sjj1312b.codec.response.HsmConvertRsaPriKeyUseKekResponse;
import com.crb.kms.hsm.sjj1312b.codec.response.HsmGenEccKeyResponse;
import com.crb.kms.hsm.sjj1312b.codec.response.HsmGenRsaPairResponse;
import com.crb.kms.hsm.sjj1312b.codec.response.HsmResponse;
import com.crb.log.Log;

public class Util {

	public static RsaPairKeyInfo genRsaPairKey(int kekIndex, int moduleLen, int exponent ,int rsaIndex) {
		try {
			/*
			 * 生成公钥对
			 */
			HsmGenRsaPairResponse rsaPairResp = HsmSJJ1312BUtil.genRsaKeyPair(moduleLen, exponent , rsaIndex ,"12345678");
			if (rsaPairResp.getStatus() != HsmResponse.RLT_SUCCESS) {
				throw new Exception("生成公钥对失败!");
			}
			System.out.println("lmk_privateKey:"+rsaPairResp.getPrivatekey());
			/*
			 * 将指定的私钥做一次转加密,采用次主密钥进行加密
			 */
			HsmConvertRsaPriKeyUseKekResponse pukResp = HsmSJJ1312BUtil.convertRsaPriKeyUseKek(
					kekIndex, rsaPairResp.getPrivatekey());
			if (pukResp.getStatus() != HsmResponse.RLT_SUCCESS) {
				throw new Exception("RSA私钥转加密失败!");
			}
			

			String pkInfo = HsmSJJ1312BUtil.desAndTdesAlg(true, false, false, null,
					pukResp.getPrivateKey(), kekIndex);
			if (pkInfo == null) {
				return null;
			}

			System.out.println("私钥DER明文:" + pkInfo);

			/*
			 * 解析DER格式,获取相应数据
			 */

			// 公钥信息
			System.out.println("PublicKey der = "+rsaPairResp.getPublicKey());
			String temp = rsaPairResp.getPublicKey();
			temp = getBerTlvV(temp);
			System.out.println("temp tlvv = "+temp);
			temp = getBerTlvV(temp);
			System.out.println("temp tlvv = "+temp);
			String publicKey = temp.replaceFirst("00", "");

			// 私钥信息
			temp = pkInfo;
			temp = getBerTlvV(temp);
			int len = 0;
			for (int i = 0; i < 3; i++) {
				len = getDataLen(temp);
				temp = temp.substring(len);
			}

			temp = getBerTlvV(temp);
			// pkInfo = HsmSJJ1312BUtil.desAndTdesAlg(true, false, true, null, temp, kekIndex);
			// if (pkInfo == null) {
			// return null;
			// }

			RsaPairKeyInfo rpkInfo = new RsaPairKeyInfo();
			System.out.println("私钥明文:" + temp);
			rpkInfo.setPrivateKey(temp);
			rpkInfo.setPublicExponent(rsaPairResp.getPublicE());
			rpkInfo.setPublicKey(publicKey);
			System.out.println("公钥明文:" + publicKey);
			rpkInfo.setPrivateKeyDer(rsaPairResp.getPrivatekey());
			return rpkInfo;
		} catch (Exception e) {
			Log.error(null, Util.class, "RsaPairKeyInfo().", e);
		}
		return null;
	}
	public static RsaPairKeyInfo genRsaPairKeyEcc(int kekIndex,int rsaIndex) {
		try {
			/*
			 * 生成公钥对
			 */
			HsmGenEccKeyResponse rsaPairResp = HsmSJJ1312BUtil.genEccKey();
			if (rsaPairResp.getStatus() != HsmResponse.RLT_SUCCESS) {
				throw new Exception("生成公钥对失败!");
			}

			/*
			 * 将指定的私钥做一次转加密,采用次主密钥进行加密
			 */
			HsmConvertRsaPriKeyUseKekResponse pukResp = HsmSJJ1312BUtil.convertRsaPriKeyUseKek(
					kekIndex, rsaPairResp.getPriKey());
			if (pukResp.getStatus() != HsmResponse.RLT_SUCCESS) {
				throw new Exception("RSA私钥转加密失败!");
			}

			String pkInfo = HsmSJJ1312BUtil.desAndTdesAlg(true, false, false, null,
					pukResp.getPrivateKey(), kekIndex);
			if (pkInfo == null) {
				return null;
			}

			System.out.println("私钥DER明文:" + pkInfo);

			/*
			 * 解析DER格式,获取相应数据
			 */

			// 公钥信息
			String temp = rsaPairResp.getPubKey();
			temp = getBerTlvV(temp);
			System.out.println(temp);
			temp = getBerTlvV(temp);
			System.out.println(temp);
			String publicKey = temp.replaceFirst("00", "");

			System.out.println("私钥明文:" + temp);
			
			// 私钥信息
			temp = pkInfo;
			temp = getBerTlvV(temp);
			int len = 0;
			for (int i = 0; i < 3; i++) {
				len = getDataLen(temp);
				temp = temp.substring(len);
			}

			temp = getBerTlvV(temp);
			// pkInfo = HsmSJJ1312BUtil.desAndTdesAlg(true, false, true, null, temp, kekIndex);
			// if (pkInfo == null) {
			// return null;
			// }

			System.out.println("私钥明文:" + temp);
			RsaPairKeyInfo rpkInfo = new RsaPairKeyInfo();
			rpkInfo.setPrivateKey(temp);
			rpkInfo.setPublicKey(publicKey);
			System.out.println("公钥明文:" + publicKey);
			return rpkInfo;
		} catch (Exception e) {
			Log.error(null, Util.class, "RsaPairKeyInfo().", e);
		}
		return null;
	}

	/**
	 * 获取BER_TLV数据
	 * @param data
	 * @return
	 */
	public static String getBerTlvV(String data) {
		int len = Integer.parseInt(data.substring(2, 4), 16);
		int lenLen = 0;
		if ((len & 0x80) == 0x80) {
			lenLen = (len & 0x7F);
			len = Integer.parseInt(data.substring(4, 4 + lenLen * 2), 16) * 2;
			return data.substring(4 + lenLen * 2, 4 + lenLen * 2 + len);
		}
		return data.substring(4, 4 + len * 2);
	}

	private static int getDataLen(String data) {
		int len = Integer.parseInt(data.substring(2, 4), 16);
		int len2 = 0;
		if ((len & 0x80) == 0x80) {
			len2 = (len & 0x7F);
			len = Integer.parseInt(data.substring(4, 4 + len2 * 2), 16) * 2;
			return len + 6;
		}
		return len * 2 + 4;
	}

}
